﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;

namespace FlashPay.Admin.Controllers
{
    using FlashPay.Entity;
    using FlashPay.Entity.Request;
    using FlashPay.Entity.Response;
    using FlashPay.Entity.Response.DepositRecord;
    using FlashPay.Service.Interface;
    using FlashPay.Util;

    public class QRCodeController : Controller
    {

        private readonly IHostingEnvironment _host;

        private readonly QRCodeService _qrCodeService;

        public QRCodeController(IHostingEnvironment host, QRCodeService qrCodeService)
        {
            _host = host;
            _qrCodeService = qrCodeService;
        }


        /// <summary>
        /// 根据订单编号获取
        /// </summary>
        /// <param name="orderNo"></param>
        /// <returns></returns>
        public JsonResult GetAlipay(AlipayRequest request)
        {
            HttpContext.Response.Headers.Add("Access-Control-Allow-Origin","*");
            HttpContext.Response.Headers.Add("Cache-Control", "no-cache");

            var response = new JResult<AlipayResponse>()
            {
                Success = false
            };

            try
            {
                //var bitmap = WebUtil.QRCode(request.Url, 5, 6, $"{_host.WebRootPath}\\images\\alipaylogo.png", 20, 5, true);

                var data =_qrCodeService.GetAlipay(request);
                if (data != null)
                {
                    
                    response.Success = true;
                    response.Data = data;
                    //data.QRCode = $"data:image/jpg;base64,{WebUtil.ImgToBase64String(bitmap)}";
                }
            }
            catch (Exception ex) {
                response.ErrorMessage = ex.Message;
            }

            return Json(response);
        }

        /// <summary>
        /// 根据订单编号获取
        /// </summary>
        /// <param name="orderNo"></param>
        /// <returns></returns>
        public JsonResult GetDepositRecord(AlipayRequest request)
        {
            HttpContext.Response.Headers.Add("Access-Control-Allow-Origin", "*");
            HttpContext.Response.Headers.Add("Cache-Control", "no-cache");

            var response = new JResult<DepositRecordResponse>()
            {
                Success = false
            };

            try
            {
                var data = _qrCodeService.GetDepositRecord(request);
                if (data != null)
                {

                    
                    response.Data = data;
                    response.Success = true;
                }
            }
            catch (Exception ex)
            {
                response.ErrorMessage = ex.Message;
            }

            return Json(response);
        }
    }
}